﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QUANLYNHASACH
{
    public partial class frmQUANLYTAIKHOAN : Form
    {
        QUANLYNHASACH.User_BLL bllUser = new QUANLYNHASACH.User_BLL();
    
        
        public frmQUANLYTAIKHOAN()
        {
            InitializeComponent();
        }

        private void frmQUANLYTAIKHOAN_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'ds_quanlybansach.GROUPS' table. You can move, or remove it, as needed.
            this.gROUPSTableAdapter.Fill(this.ds_quanlybansach.GROUPS);
            GridUser.DataSource = bllUser.LayDanhSachUser(); 
         


        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            string idUsr = bllUser.CreateID() ;
            string idGroup = cbbQuyen.SelectedValue.ToString() ;
            string UserName=txtUser.Text;
            string Password=txtPassword.Text;
            string Hoten=txtHoten.Text;
            bllUser.ThemUser(idUsr, idGroup, UserName, Password, Hoten);
            GridUser.DataSource= bllUser.LayDanhSachUser();

        }

        private void btnSua_Click(object sender, EventArgs e)
        {

            bool isCheck = false;
            for (int i = 0; i < this.GridUser.Rows.Count; i++)
            {
                if (this.GridUser.Rows[i].Cells[0].Value != null)
                {
                    isCheck = (bool)this.GridUser.Rows[i].Cells[0].Value;
                    string idUser = this.GridUser.Rows[i].Cells[1].Value.ToString();
                    string idGroup = this.GridUser.Rows[i].Cells[2].Value.ToString();
                    string UserName = this.GridUser.Rows[i].Cells[3].Value.ToString();
                    string Password = this.GridUser.Rows[i].Cells[4].Value.ToString();
                    string HoTen = this.GridUser.Rows[i].Cells[5].Value.ToString();
                   
                    if (isCheck == true)
                    {
                        bllUser.CapNhatUser(idUser,idGroup,UserName,Password,HoTen);
                    }
                    GridUser.DataSource= bllUser.LayDanhSachUser();
                }
            }
            MessageBox.Show("Đã cập nhật danh sách user thành công", "Thông báo");
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            bool isCheck = false;
            for (int i = 0; i < this.GridUser.Rows.Count; i++)
            {
                if (this.GridUser.Rows[i].Cells[0].Value != null)
                {
                    isCheck = (bool)this.GridUser.Rows[i].Cells[0].Value;
                    string idNXB = this.GridUser.Rows[i].Cells[1].Value.ToString();

                    if (isCheck == true)
                    {
                       bllUser.XoaUser(idNXB);

                    }
                    GridUser.DataSource = bllUser.LayDanhSachUser();
                }
            }
        }

    }
}
